home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / UCRASM25.ARJ / ATOL.ASM < prev    next >
Assembly Source File  |  1991-10-12  |  2KB  |  129 lines

  1. StdGrp        group    stdlib,stddata
  2. stddata        segment    para public 'sldata'
  3. stddata        ends
  4. ;
  5. stdlib        segment    para public 'slcode'
  6.         assume    cs:stdgrp
  7. ;
  8. ;
  9. ; ATOL-    Converts the string pointed at by ES:DI to a signed long integer value
  10. ;    and returns this integer in the DX:AX registers.
  11. ;
  12. ;    Returns with the carry flag clear if no error, set if overflow.
  13. ;
  14.         public    sl_atol
  15. sl_atol        proc    far
  16.         push    di
  17.         call    far ptr sl_atol2
  18.         pop    di
  19.         ret
  20. sl_atol        endp
  21. ;
  22. ;
  23.         public    sl_atol2
  24. sl_atol2    proc    far
  25.         push    cx
  26.         xor    cx, cx
  27.         mov    dx, cx
  28.         mov    ah, ch                ;Assume it's positive
  29.         cmp    byte ptr es:[di], '-'
  30.         jne    DoAtoI
  31. ;
  32. ; Set up for negative numbers.
  33. ;
  34.         inc    di                ;Skip "-"
  35.         mov    ah, 1                ;Flag negative value.
  36. ;
  37. DoAtoI:        call    NAtoI
  38.         jc    WasError            ;Quit if error.
  39.         cmp    ah, 0
  40.         je    IsPositive
  41.         neg    dx
  42.         neg    cx
  43.         sbb    dx, 0
  44.         clc
  45.         jmp    WasError            ;Not really an error.
  46. ;
  47. IsPositive:    or    cx, cx                ;See if overflow
  48.         clc
  49.         jns    WasError            ;Not an error
  50.                 stc                    ;Error if negative.
  51. WasError:    mov    ax, cx
  52.         pop    cx
  53.         ret
  54. sl_atol2    endp
  55. ;
  56. ;
  57. ;
  58. ; ATOUL-    Just like ATOL but this guy only does unsigned numbers.
  59. ;
  60.         public    sl_atoul
  61. sl_atoul    proc    far
  62.         push    di
  63.         call    far ptr sl_atoul2
  64.         pop    di
  65.         ret
  66. sl_atoul    endp
  67. ;
  68. ;
  69.         public    sl_atoul2
  70. sl_atoul2    proc    far
  71.         push    cx
  72.         xor    cx, cx
  73.         mov    dx, cx
  74.         call    NAtoI
  75.         mov    ax, cx
  76.         pop    cx
  77.         ret
  78. sl_atoul2    endp
  79. ;
  80. ;
  81. ;
  82. ;
  83. NAtoI        proc    near
  84.         push    bx
  85.         push    si
  86.         pushf
  87.         cld
  88. ;
  89. lp:        mov    al, es:[di]        ;Get byte at es:si
  90.         inc    di
  91.         xor    al, '0'
  92.         cmp    al, 10
  93.         ja    NotDigit
  94.         shl    cx, 1
  95.         rcl    dx, 1
  96.         jc    Error
  97.         mov    bx, cx
  98.         mov    si, dx
  99.         shl    cx, 1
  100.         rcl    dx, 1
  101.         jc    Error
  102.         shl    cx, 1
  103.         rcl    dx, 1
  104.         jc    Error
  105.         add    cx, bx
  106.         adc    dx, si
  107.         jc    Error
  108.         add    cl, al
  109.         adc    ch, 0
  110.         adc    dx, 0
  111.         jc    Error
  112.         jmp    lp
  113. ;
  114. NotDigit:    popf
  115.         pop    si
  116.         pop    bx
  117.         clc
  118.         ret
  119. ;
  120. Error:        popf
  121.         pop    si
  122.         pop    bx
  123.         stc
  124.         ret
  125. NAtoI        endp
  126. ;
  127. stdlib        ends
  128.         end
  129.